package com.zyh.demo.reflect;

import com.zyh.demo.domain.Book;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;

/**
 * @Author zyh
 * @Date 2024/8/14 11:08
 * @Description
 */

public class ReflectTest2 {
    /*
    构造方法的操作
     */
    @Test
    public void test3()throws Exception{
        Class c = Book.class;
        //拿到满参构造方法
        Constructor cons = c.getDeclaredConstructor(String.class, String.class, Double.class, Date.class);
        //获取满参构造方法的权限修饰符
        int modifiers = cons.getModifiers();
        System.out.println("modifiers = " + modifiers);
        //获取满参构造方法的方法名
        System.out.println(cons.getName());
        //获取满参构造方法的参数个数
        int parameterCount = cons.getParameterCount();
        System.out.println("parameterCount = " + parameterCount);
        //获取满参构造方法的参数类型依次存入数组
        Class[] parameterTypes = cons.getParameterTypes();
        System.out.println(Arrays.toString(parameterTypes));
        //调用满参构造方法
        Book book = (Book) cons.newInstance("三国演义", "zyh", 30.9, new Date());
        Method toString = c.getDeclaredMethod("toString");
        System.out.println(toString.invoke(book));
    }

    /*
    方法的操作
     */
    @Test
    public void test2() throws Exception{
        Class c = Book.class;
        //获取一个方法
        Method setbNameMethod = c.getDeclaredMethod("setbName", String.class);
        //拿到方法的权限修饰符
        int modifiers = setbNameMethod.getModifiers();
        System.out.println("modifiers = " + modifiers);//modifiers = 1
        //拿到方法的返回值类型
        Class returnType = setbNameMethod.getReturnType();
        System.out.println("returnType = " + returnType);//returnType = void
        //获取方法名
        System.out.println(setbNameMethod.getName());//setbName
        //获取形参个数
        int parameterCount = setbNameMethod.getParameterCount();
        System.out.println("parameterCount = " + parameterCount);//parameterCount = 1
        //获取形参类型依次存入数组
        Class[] parameterTypes = setbNameMethod.getParameterTypes();
        System.out.println(Arrays.toString(parameterTypes));//[class java.lang.String]
        //调用方法
        //打开暴力访问开关
        setbNameMethod.setAccessible(true);
        Book book = new Book("红楼梦", "zyh", 21.2, new Date());
        setbNameMethod.invoke(book,"java从入门到放弃");
        Method toString = c.getDeclaredMethod("toString");
        System.out.println(toString.invoke(book));//Book{bName='java从入门到放弃', author='zyh', price=21.2, createTime=Wed Aug 14 11:44:58 CST 2024}
    }

    /*
    属性的操作
     */
    @Test
    public void test1() throws Exception {
        Class c = Book.class;
        Field authorField = c.getDeclaredField("author");
        //获取属性的权限修饰符
        int modifiers = authorField.getModifiers();
        System.out.println("modifiers = " + modifiers);
        //获取属性的属性值类型
        String name = authorField.getType().getName();
        System.out.println("name = " + name);
        //获取属性的属性值
        String authorFieldName = authorField.getName();
        System.out.println("authorFieldName = " + authorFieldName);
        //打开暴力访问开关
        authorField.setAccessible(true);
        //获取属性值
        Book book=new Book("西游记","zyh",19.9,new Date());
        String fieldValue = (String) authorField.get(book);
        System.out.println("fieldValue = " + fieldValue);
        //修改属性值
        authorField.set(book,"吴承恩");
        System.out.println("book = " + book);
    }
}
